<?php


namespace app\user\controller;


use think\Controller;
use think\Db;
use think\facade\Request;
use app\common\model\Product as ProductModel;

class Product extends  Controller
{

    //将测试题库的题目移入正式版题库
    public function moveTestToProduct($id )
    {
//        $id=2031;
        $data=Db::table('test_data')->where('id',$id)->find();

        //目标数组
        $list=[];

        //将数据转化为数组
        $old_list= json_decode($data['data']) ;


        foreach ($old_list as $key => $item) {

            //选项数组
            $a=[];

            foreach ($item->testAnswerDtos as $key2=> $item2) {

                //如果为数字，表示跳转到选项，如果为字母，表示跳转到答案
                if(is_numeric($item2->jumpAnsId)){
                    $type='opt';
                }else{
                    $type='answer';
                }

                array_push($a,[
                    'opt'=>$item2->content,
                    'jumpAnsIndex'=> $item2->jumpAnsId,
                    'value'=>0,
                    'to'=>$type
                ]);
            }

            array_push($list,[
                "q"=>$item->content,
                "a"=>$a
            ]);

        }

         $res=[
             'reslut'=>json_decode($data['answer']) ,
             'list'=>  $list
         ];

        $res2=Db::table('product')->insert([
            'name'=>$data['name'],
            'test_type'=>'jump',
            'decs'=>$data['tipsTitle'],
            'data'=>json_encode($res,JSON_UNESCAPED_UNICODE)
        ]);

         $this->result($res,200);
    }

    //获取产品列表
    public function index()
    {
        $page=Request::param('page',1);
        $list=ProductModel::where(['status'=>1])->page($page,20)->field(['data'],true)->select();

        for ($i=0;$i<count($list);$i++){
             $list[$i]['user_rate']=0.9;
             $list[$i]['vip_rate']=1;
        }

        $this->result($list,200,'成功');
    }


}
